package com.spr.controller;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.spr.model.User;
import com.spr.service.UserService;



@Controller
@RequestMapping(value="/user")
public class UserController {
    @Autowired
    private UserDetailsService userDetailsService;
    
    @Autowired
    private UserService userService;
    
//    @Autowired
//    private UserValidator userValidator;
//    
//    @InitBinder
//    private void initBinder(WebDataBinder binder) {
//            binder.setValidator(userValidator);
//    }
    
    @RequestMapping(value="/register", method=RequestMethod.GET)
    public ModelAndView newUserPage(HttpSession session) {
            ModelAndView mav = new ModelAndView("user-new", "user", new User());
            return mav;
    }
    
    @RequestMapping(value="/register", method=RequestMethod.POST)
    public ModelAndView createNewUser(@ModelAttribute @Valid User user,
                    BindingResult result,
                    final RedirectAttributes redirectAttributes, HttpSession session) {
            
            if (result.hasErrors())
                    return new ModelAndView("user-new");
            
            ModelAndView mav = new ModelAndView();
            
            userService.create(user);
            
            User ud = (User) userDetailsService.loadUserByUsername(user.getUsername());
            
            session.setAttribute("UserDetails", ud);
            
            mav.setViewName("redirect:/index.html");
      
            return mav;                
    }
    
    @RequestMapping(value="/login", method=RequestMethod.GET)
    public ModelAndView loginUserPage(HttpSession session) {
            ModelAndView mav = new ModelAndView("user-login", "user", new User());
            return mav;
    }
    
    @RequestMapping(value="/login", method=RequestMethod.POST)
    public ModelAndView loginUser(@ModelAttribute @Valid User user,
                    BindingResult result,
                    final RedirectAttributes redirectAttributes, HttpSession session) {
            
            if (result.hasErrors())
                    return new ModelAndView("user-login");
            
            ModelAndView mav = new ModelAndView();
            User ud = null;
            try {
                ud = (User) userDetailsService.loadUserByUsername(user.getUsername());
			} catch (Exception e) {
				mav.setViewName("redirect:/user/login");
				return mav;
			}
            
            if(user.getPassword().equals(ud.getPassword()) && user.getUsername().equals(ud.getUsername()))
            {
                session.setAttribute("UserDetails", ud);
                
                mav.setViewName("redirect:/home/");
            }
            else {
            	mav.setViewName("redirect:/user-login.html");
			}
            
      
            return mav;                
    }
}
